package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.GoodsInfoLog;
import com.cn.goodsadmin.po.OrderStatistics;
import com.cn.goodsadmin.service.ActionRoutService;
import com.cn.goodsadmin.service.OrderService;
import com.cn.goodsadmin.service.UserBuyLogService;
import com.cn.goodsadmin.service.UserService;
import com.cn.goodsadmin.util.ErrorMsg;
import com.cn.goodsadmin.util.MoneyUtil;
import com.cn.goodsadmin.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

//@CrossOrigin(origins = {"http://10.10.10.143:8020"}, maxAge = 3600)
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class OperateAnalysisController {

    @Autowired
    OrderService orderService;
    @Autowired
    ActionRoutService actionRoutService;
    @Autowired
    UserService userService;
    @Autowired
    UserBuyLogService userBuyLogService;

    /**
     * 获取定义天数的每个品类的销售情况
     *
     * @param day
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/getGoodsInfoLog", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getGoodslog(@RequestParam(value = "day") int day, @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<GoodsInfoLog> goodsInfoLogs = userBuyLogService.findGoodsCountBytime(day, appid);
            long count = 0;
            for (GoodsInfoLog goodsInfoLog : goodsInfoLogs) {
                count += goodsInfoLog.getTotal();
            }
            for (GoodsInfoLog goodsInfoLog : goodsInfoLogs) {
                //System.out.println(coffeeLog.getTotal()/count);
                goodsInfoLog.per = MoneyUtil.coverTwoMoney((double) goodsInfoLog.getTotal() / (double) count);
            }
            supplyMap.put("goodslog", goodsInfoLogs);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    /**
     * 当coffeeid 为null的时候 就是 分析 每天每个时间段的杯数
     * 当coffeeid 不为null的时候 就是 分析 这个品类 每个时间段的杯数
     *
     * @param day
     * @param goodsid
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/getGoodsTimeDetail", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getGoodsTime(@RequestParam(value = "day") int day, @RequestParam(value = "goodsid", required = false) String goodsid, @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            HashMap<String, Integer> map = userBuyLogService.getGoodsTimeLogByTime(day, goodsid, appid);
            if (map != null) {
                supplyMap.put("timemap", map);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    /**
     * 以时间为维度的统计 比如 16:00 这个时间每个品类卖了多少杯
     *
     * @param day
     * @param time
     * @return
     */
    @LoginRequired
    @RequestMapping(value = "/getTimeStatistics", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getTimeStatistics(@RequestParam(value = "day") int day,
                                       @RequestParam(value = "time") String time,
                                       @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            HashMap<String, Integer> map = userBuyLogService.getGoodInfoOneTimeCount(time, day, appid);
            if (map != null) {
                supplyMap.put("TimeStatistics", map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/getOrderStatistics", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getOrderStatistics(@RequestParam(value = "day") int day,
                                        @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            OrderStatistics orderStatistics = orderService.getAllOrderStaticeByTime(day, 1, appid);
            if (orderStatistics != null) {
                supplyMap.put("OrderStatistics", orderStatistics);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getLoginLog", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getLoginLog(@RequestParam(value = "day") int day, @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            TreeMap<String, Integer> map = actionRoutService.getActionRoutByDay(day, appid);
            if (map != null) {
                supplyMap.put("actionrout", map);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getLoginLogDetail", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getLoginLogDetail(@RequestParam(value = "day") String day, @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            Date date = TimeUtil.coverFormatToDateNoHm(day);
            HashMap<String, Long> map = userService.analyNewAndOldUser(date, appid);
            if (map != null) {
                supplyMap.put("analy", map);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getActivityUserLog", method = RequestMethod.GET)
    @ResponseBody
    public SupplyMap getActivityUserLog(@RequestParam(value = "bday") String bday,
                                        @RequestParam(value = "eday") String eday,
                                        @RequestParam(value = "appid") int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            Date bdate = TimeUtil.coverFormatToDateNoHm(bday);
            Date edate = TimeUtil.coverFormatToDateNoHm(eday);
            HashMap<String, Long> map = userService.getUserActivityLog(bdate, edate, appid);
            if (map != null) {
                supplyMap.put("map", map);
                supplyMap.put("size", map.size());
                Iterator iterable = map.entrySet().iterator();
                long activitynumber = 0;
                while (iterable.hasNext()) {
                    Map.Entry entry = (Map.Entry) iterable.next();
                    Long number = (Long) entry.getValue();
                    if (number > 1) {
                        activitynumber++;
                    }
                }
                supplyMap.put("activity", activitynumber);
            }
        } catch (Exception e) {
            e.printStackTrace();
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
